﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using SalePartial.BLL;
using SalePartial.Utility;
using System.Data;
using Telerik.Web.UI;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class Trade_Events_event_grid : System.Web.UI.Page
{
    
    protected void Page_Load(object sender, System.EventArgs e)
    {

       
        if (!IsPostBack)
        {
            ////■■■■■テストため■■■■
            //UserBLL.StoreUserInfoToSession("j@j10.com");
            LoginUserType type = RoleControlManager.checkUserAuthority(User.Identity.Name);
            if (type == LoginUserType.FREE_PURCHASE
                || type == LoginUserType.CERT_PURCHASE || type == LoginUserType.CERT_SUP_PUR
                || type == LoginUserType.FREE_SUP_PUR)
            {
                if (Session["s_userID_guid"] == null || Session["s_companyID_guid"] == null)
                {
                    bool result = UserBLL.StoreUserInfoToSession(User.Identity.Name);
                }

                sdsInquiryList.SelectParameters["CompanyID"].DefaultValue = Session["s_companyID_guid"].ToString();
                sdsInquiryList.FilterParameters["Filter1"].DefaultValue = "CompanyID='" + sdsInquiryList.SelectParameters["CompanyID"].DefaultValue+"'";
                
                string displaytext = rpbNavigator.FindItemByValue("All").Text;

                for (int i = 0; i <= 10; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("All").Text = displaytext;
                displaytext = rpbNavigator.FindItemByValue("UnApproval").Text;

                for (int i = 0; i <= 10; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("UnApproval").Text = displaytext;

                displaytext = rpbNavigator.FindItemByValue("UnQuote").Text;
                for (int i = 0; i <= 10; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("UnQuote").Text = displaytext;
                displaytext = rpbNavigator.FindItemByValue("UnAssigned").Text;
                for (int i = 0; i <= 10; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("UnAssigned").Text = displaytext;

                displaytext = rpbNavigator.FindItemByValue("Quoted").Text;
                for (int i = 0; i <= 10; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("Quoted").Text = displaytext;
                displaytext = rpbNavigator.FindItemByValue("Successed").Text;
                for (int i = 0; i <= 10; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("Successed").Text = displaytext;

                displaytext = rpbNavigator.FindItemByValue("Assigned").Text;
                for (int i = 0; i <= 10; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("Assigned").Text = displaytext;

                displaytext = rpbNavigator.FindItemByValue("Feedback").Text;
                for (int i = 0; i <= 10; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("Feedback").Text = displaytext;

                displaytext = rpbNavigator.FindItemByValue("ExpiredToday").Text;
                for (int i = 0; i <= 10; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("ExpiredToday").Text = displaytext;

                displaytext = rpbNavigator.FindItemByValue("ExpiredTomm").Text;
                for (int i = 0; i <= 10; i++)
                {
                    displaytext = displaytext.Insert(displaytext.IndexOf(" "), "&nbsp;&nbsp;");
                }
                rpbNavigator.FindItemByValue("ExpiredTomm").Text = displaytext;


                InitializeEventRecordsOnMenuPanel();

                //FormsAuthentication.SetAuthCookie("xylibra@tom.com", False)
            }
            else
            {
                Response.Redirect(GlobalSetting.UrlHomePage, true);

            }
        }

    }

    private void InitializeEventRecordsOnMenuPanel()
    {
        string displaytext;
        DataView dv = (DataView)sdsInquiryList.Select(DataSourceSelectArguments.Empty);
        
        displaytext = rpbNavigator.FindItemByValue("All").Text;
        dv.RowFilter = "(SUBSTRING([InquiryStatus], 2, 1) = '1') ";
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("All").Text = displaytext;


        displaytext = rpbNavigator.FindItemByValue("UnApproval").Text;
        dv.RowFilter = "(SUBSTRING([InquiryStatus], 2, 2) = '10') ";
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("UnApproval").Text = displaytext;



        displaytext = rpbNavigator.FindItemByValue("UnAssigned").Text;
        dv.RowFilter = "(SUBSTRING([InquiryStatus], 2, 4) = '1110') ";
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("UnAssigned").Text = displaytext;





        SqlConnection conns = new SqlConnection("Data Source=406-PC\\SQLEXPRESS;Initial Catalog=XunPan;User ID=XunPan_sa;Password=123456");
        SqlCommand comms = new SqlCommand("select InquiryID from [XunPan].[dbo].[XPQuote]   where substring([QuoteStatus],7,1)='1' and substring([QuoteStatus],4,1)='1'", conns);
        conns.Open();

        SqlDataReader bb = comms.ExecuteReader();
        displaytext = rpbNavigator.FindItemByValue("Successed").Text;
        string sql="";
        while (bb.Read())
        {   
            sql = "   [InquiryID] = '" + bb["InquiryID"] + "' or "+sql;
        }

        if (sql == "")
            dv.RowFilter = "  (SUBSTRING([InquiryStatus], 2, 4) = '1111'and [InquiryID] = '')";
        else
        {
            sql = sql.Remove(sql.LastIndexOf("or"));
            dv.RowFilter = "(SUBSTRING([InquiryStatus], 2, 4) = '1111' and (" + sql + "))";
        }
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("Successed").Text = displaytext;
       


        displaytext = rpbNavigator.FindItemByValue("UnQuote").Text;
        dv.RowFilter = "  (SUBSTRING([InquiryStatus], 2, 3) = '110') ";
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("UnQuote").Text = displaytext;

        displaytext = rpbNavigator.FindItemByValue("Quoted").Text;
        dv.RowFilter = "  (SUBSTRING([InquiryStatus], 2, 3) = '111') ";
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("Quoted").Text = displaytext;

        SqlConnection conn = new SqlConnection("Data Source=406-PC\\SQLEXPRESS;Initial Catalog=XunPan;User ID=XunPan_sa;Password=123456");
        SqlCommand comm = new SqlCommand("select InquiryID from [XunPan].[dbo].[XPQuote]   where substring([QuoteStatus],7,1)='0' and substring([QuoteStatus],4,1)='1'", conn);
        conn.Open();

        SqlDataReader aa = comm.ExecuteReader();
      
        displaytext = rpbNavigator.FindItemByValue("Feedback").Text;
        string sqls = "";
        while (aa.Read())
        {
            sqls = "   [InquiryID] = '" + aa["InquiryID"] + "'  or " + sqls;
        }

        if (sqls == "")
            dv.RowFilter = "  (SUBSTRING([InquiryStatus], 2, 4) = '1111'and [InquiryID] = '')";
        else
        {
            sqls = sqls.Remove(sqls.LastIndexOf("or"));
            dv.RowFilter = "  (SUBSTRING([InquiryStatus], 2, 4) = '1111' and (" + sqls + "))";
        }
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("Feedback").Text = displaytext;
      

        dv.RowFilter = "  (SUBSTRING([InquiryStatus], 2, 4) = '1111') ";
        displaytext = rpbNavigator.FindItemByValue("Assigned").Text;
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("Assigned").Text = displaytext;


        dv.RowFilter = "[ExpireDate] = '" + System.DateTime.Today.ToString("d") + "'";
        displaytext = rpbNavigator.FindItemByValue("ExpiredToday").Text;
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("ExpiredToday").Text = displaytext;

        dv.RowFilter = "[ExpireDate] = '" + System.DateTime.Today.AddDays(1).ToString("d") + "'";
        displaytext = rpbNavigator.FindItemByValue("ExpiredTomm").Text;
        displaytext = displaytext.Remove(displaytext.LastIndexOf(" ") + 1);
        displaytext = displaytext + dv.Count.ToString();
        rpbNavigator.FindItemByValue("ExpiredTomm").Text = displaytext;
        conn.Close();
        conns.Close();

    }

    protected void rpbNavigator_ItemClick(object sender, Telerik.Web.UI.RadPanelBarEventArgs e)
    {
        {
            if (e.Item.Items.Count > 0)
            {
                //主菜单框
                //设置询盘审核状态 InquiryStatus 第3位   同意是1 拒绝是2
                //设置询盘发布状态     InquiryStatus 第2位 询盘发布为1
                //设置报价状态     InquiryStatus 第3位 已报价为1
                //设置授盘状态     InquiryStatus 第5位 已授盘发布为1
            }
            else
            {
                //条目框

                if (e.Item.Value != null && !string.IsNullOrEmpty(e.Item.Value))
                {

                    
                    switch (e.Item.Value)
                    {

                        case "All":
                            sdsInquiryList.FilterParameters["Filter1"].DefaultValue = "CompanyID='" + sdsInquiryList.SelectParameters["CompanyID"].DefaultValue +"'and  (SUBSTRING([InquiryStatus], 2, 1) = '1') ";
                            break;
                        case "UnApproval":
                            sdsInquiryList.FilterParameters["Filter1"].DefaultValue = "CompanyID='" + sdsInquiryList.SelectParameters["CompanyID"].DefaultValue +"'and  (SUBSTRING([InquiryStatus], 2, 2) = '10') ";
                            break;

                        case "UnAssigned":
                            sdsInquiryList.FilterParameters["Filter1"].DefaultValue = "CompanyID='" + sdsInquiryList.SelectParameters["CompanyID"].DefaultValue + "'and  (SUBSTRING([InquiryStatus], 2, 4) = '1110') ";
                            break;
                        case "Successed":
                            
                                SqlConnection conns = new SqlConnection("Data Source=406-PC\\SQLEXPRESS;Initial Catalog=XunPan;User ID=XunPan_sa;Password=123456");
                                SqlCommand comms = new SqlCommand("select InquiryID from [XunPan].[dbo].[XPQuote]   where substring([QuoteStatus],7,1)='1' and substring([QuoteStatus],4,1)='1'", conns);
                                conns.Open();

                                SqlDataReader bb = comms.ExecuteReader();
                                string sql = "";
                            while (bb.Read())
                            {
                                sql = "  [InquiryID] = '" + bb["InquiryID"] + "' or"+sql;
                                
                            }

                            if (sql == "")
                                sdsInquiryList.FilterParameters["Filter1"].DefaultValue = "CompanyID='" + sdsInquiryList.SelectParameters["CompanyID"].DefaultValue + "'and   (SUBSTRING([InquiryStatus], 2, 4) = '1111' and [InquiryID]='')";
                            else
                            {
                                sql = sql.Remove(sql.LastIndexOf("or"));
                                sdsInquiryList.FilterParameters["Filter1"].DefaultValue = "CompanyID='" + sdsInquiryList.SelectParameters["CompanyID"].DefaultValue + "'and   (SUBSTRING([InquiryStatus], 2, 4) = '1111' and (" + sql + ")) ";
                            }
                            conns.Close();
                            break;
                        case "UnQuote":
                            sdsInquiryList.FilterParameters["Filter1"].DefaultValue = "CompanyID='" + sdsInquiryList.SelectParameters["CompanyID"].DefaultValue + "'and   (SUBSTRING([InquiryStatus], 2, 3) = '110') ";
                            break;
                        case "Quoted":
                            sdsInquiryList.FilterParameters["Filter1"].DefaultValue = "CompanyID='" + sdsInquiryList.SelectParameters["CompanyID"].DefaultValue + "'and   (SUBSTRING([InquiryStatus], 2, 3) = '111') ";
                            break;
                        case "Assigned":
                            sdsInquiryList.FilterParameters["Filter1"].DefaultValue = "CompanyID='" + sdsInquiryList.SelectParameters["CompanyID"].DefaultValue + "'and    (SUBSTRING([InquiryStatus], 2, 4) = '1111') ";
                            break;
                        case "Feedback":
                            string sqls = "";
                            
                    SqlConnection conn = new SqlConnection("Data Source=406-PC\\SQLEXPRESS;Initial Catalog=XunPan;User ID=XunPan_sa;Password=123456");
                    SqlCommand comm = new SqlCommand("select InquiryID from [XunPan].[dbo].[XPQuote]   where substring([QuoteStatus],7,1)='0' and substring([QuoteStatus],4,1)='1'", conn);
                    conn.Open();

                    SqlDataReader aa = comm.ExecuteReader();
                            
                          while (aa.Read())
                                {
                                    sqls = "[InquiryID] = '" + aa["InquiryID"] + "'or  " + sqls;
                                }

                          if (sqls == "")
                              sdsInquiryList.FilterParameters["Filter1"].DefaultValue = "CompanyID='" + sdsInquiryList.SelectParameters["CompanyID"].DefaultValue + "'and  (SUBSTRING([InquiryStatus], 2, 4) = '1111' and [InquiryID]='')";
                          else
                          {
                              sqls = sqls.Remove(sqls.LastIndexOf("or"));
                              sdsInquiryList.FilterParameters["Filter1"].DefaultValue = "CompanyID='" + sdsInquiryList.SelectParameters["CompanyID"].DefaultValue + "'and    (SUBSTRING([InquiryStatus], 2, 4) = '1111' and (" + sqls + ")) ";
                          }
                          conn.Close();

                             break;
                        case "ExpiredToday":
                             sdsInquiryList.FilterParameters["Filter1"].DefaultValue = "CompanyID='" + sdsInquiryList.SelectParameters["CompanyID"].DefaultValue + "'and    [ExpireDate] = '" + DateTime.Today.ToString("d") + "' ";
                            break;
                        case "ExpiredTomm":
                            sdsInquiryList.FilterParameters["Filter1"].DefaultValue = "CompanyID='" + sdsInquiryList.SelectParameters["CompanyID"].DefaultValue + "'and    [ExpireDate] = '" + DateTime.Today.AddDays(1).ToString("d") + "' ";
                            break;
                        default:
                            return;
                            //conn.Close();
                            //conns.Close();
                    }
                    radGridMainlist.Rebind();
                    
                }
            }

        }
    }

    protected void radGridMainlist_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        switch (e.CommandName.ToLower())
        {

            case "viewquotes":
                string inquiryID = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["InquiryID"].ToString();
                //HttpContext.Current.Items.Add("InquiryID", inquiryID);
                HttpContext.Current.Session["InquiryID"] = inquiryID;
                Response.Redirect(e.CommandArgument.ToString(), false);
                //Server.Transfer(e.CommandArgument.ToString(), false);

                break;


            case "assign":

                string qid = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["QuoteID"].ToString();

                string xpid = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["InquiryID"].ToString();

                int result = ModerateBLL.AssignXP(xpid, qid);

                break;
            case "expandcollapse":
                e.Item.OwnerTableView.EditMode = GridEditMode.EditForms;

                break;

            case "delete":
                try
                {
                    XPQuote quote = new XPQuote();
                    quote.DeleteByInquiryID(new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["InquiryID"].ToString()));

                    XPInquiryEvent xpinquiryEve = new XPInquiryEvent();
                    xpinquiryEve.DeleteByInquiryID(new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["InquiryID"].ToString()));

                    XPInquiry xpinquiry = new XPInquiry();
                    xpinquiry.Delete(new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["InquiryID"].ToString()));


                }
                catch (Exception ex)
                {
                    return;

                }
                break;
        }
    }

    protected bool EnableAssign(string qstatus)
    {
        bool enable = false;
        try
        {
            //检查已授盘状态和未反馈状态
            if (qstatus.Substring(3, 1) == "0")
            {
                enable = true;
            }
        }
        catch (Exception ex)
        {
            enable = false;
        }

        return enable;

    }

}

